home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Doors_System / Checkfiles / Main.C < prev    next >
C/C++ Source or Header  |  1996-10-31  |  14KB  |  547 lines

  1. /*
  2.  
  3.   CheckFiles
  4.   ==========
  5.  
  6.   Called by Protocols after a file has been uploaded if the protocol is receiving
  7.   more than one file at a time, or by the UPLOAD or DOWNLOAD door after a transfer
  8.   has completed.
  9.  
  10.   Options
  11.   =======
  12.  
  13.     N_ND->ActiveDoor->SystemOptions
  14.     -------------------------------
  15.  
  16.     No options used at this time
  17.  
  18.  
  19.     Door Options (Command Line)
  20.     ---------------------------
  21.  
  22.     ALL|SINGLE
  23.  
  24.     [FILENAME]
  25.  
  26.  
  27.     Example
  28.     -------
  29.  
  30.     CheckFiles ALL
  31.  
  32.     CheckFiles SINGLE BANANA.LHA
  33.  
  34.  
  35.     Specifiying the ALL parameter causes CheckFiles to get a list of FILES in the
  36.     playpen (directories are purposely ignored!) and check each one individually.
  37.  
  38.   todo (*C*)
  39.   ====
  40.  
  41.   add tonnes of errorchecking to the allocation of memory, esp. in the direcotry
  42.   listing of the playpen dir..
  43.  
  44.   Structure
  45.   =========
  46.  
  47.   Extract_DIZ
  48.  
  49.   copy .DIZ to .ADD, .ADD is added back into the file after all the doors have finished
  50.   playing around with it,  .DIZ is added to the file lists.
  51.  
  52.   Modify_DIZ
  53.  
  54.   Check_Corrupt
  55.  
  56.   if "CORRUPT"
  57.     MoveFile BAD
  58.     AddDIZToList BAD
  59.     Update_DIZ
  60.   else
  61.     Confirm_UL
  62.     DupeCheck
  63.  
  64.     if Not "INCORRECT" and Not "DUPLICATE"
  65.       Ask_Conf
  66.       AddDIZToList <confnum>
  67.       MoveFile <cofnum>
  68.       Update_DIZ
  69.       AddCreds <confnum> <filename>
  70.     else
  71.       AddDIZToList "SYSOP" or "HOLD"
  72.       MoveFile "HOLD"
  73.       Update_DIZ
  74.     endif
  75.   endif
  76.  
  77. */
  78. #include <exec/types.h>
  79. #include <exec/memory.h>
  80. #include <dos/dos.h>
  81. #include <clib/exec_protos.h>
  82. #include <clib/dos_protos.h>
  83. #include <clib/alib_protos.h>
  84.  
  85. #include <stdlib.h>
  86. #include <string.h>
  87. #include <stdio.h>
  88. #include <ctype.h>
  89. #include <time.h>
  90.  
  91.  
  92. #ifdef __SASC
  93. int CXBRK(void) { return(0); }
  94. int _CXBRK(void) { return(0); }
  95. void chkabort(void) {}
  96. #endif
  97.  
  98. #include <HBBS/ANSI_Codes.h>
  99. #include <HBBS/Defines.h>
  100. #include <HBBS/types.h>
  101. #include <HBBS/structures.h>
  102. #include <HBBS/hbbscommon_protos.h>
  103. #include <HBBS/hbbscommon_pragmas.h>
  104. #include <HBBS/Hbbsnode_protos.h>
  105. #include <HBBS/Hbbsnode_pragmas.h>
  106. #include <HBBS/release.h>
  107. char *versionstr="$VER: CheckFiles "RELEASE_STR;
  108.  
  109. struct Library *HBBSCommonBase=NULL;
  110. struct Library *HBBSNodeBase=NULL;
  111.  
  112. long __stack=40000;
  113.  
  114. struct BBSGlobalData *BBSGlobal=NULL;
  115. struct NodeData *N_ND=NULL;
  116. int N_NodeNum=-1;
  117. char outstr[1024]; // temp string for displaying text..
  118.  
  119. BOOL checkall=FALSE;
  120. struct List *FileList=NULL;
  121. LONG Files=0;
  122.  
  123. char *file_name=NULL;
  124. LONG file_size=0;
  125. BOOL file_corrupt;
  126. BOOL file_sysop;
  127. BOOL file_incorrect;
  128. LONG file_conf;
  129. BOOL UserAround=TRUE;
  130.  
  131. static VOID cleanup(ULONG num)
  132. {
  133.   if (HBBSNodeBase)
  134.   {
  135.     HBBS_CleanUpDoor();
  136.     CloseLibrary (HBBSNodeBase);
  137.   }
  138.  
  139.   if (HBBSCommonBase)
  140.   {
  141.     HBBS_CleanUpCommon();
  142.     CloseLibrary (HBBSCommonBase);
  143.   }
  144.  
  145.   if (num) printf("Door Error = %d\n",num);
  146.  
  147.   exit(0);
  148. }
  149.  
  150. static VOID init(char *name)
  151. {
  152.   if(!(HBBSCommonBase = OpenLibrary("HBBSCommon.library",0)))
  153.   {
  154.     cleanup(1);
  155.   }
  156.  
  157.   if (!(HBBS_InitCommon()))
  158.   {
  159.     cleanup(2);
  160.   }
  161.  
  162.   if(!(HBBSNodeBase = OpenLibrary("HBBSNode.library",0)))
  163.   {
  164.     cleanup(3);
  165.   }
  166.  
  167.   if (!(HBBS_InitDoor(N_NodeNum,name)))
  168.   {
  169.     cleanup(4);
  170.   }
  171.   SetProgramName(name);
  172. }
  173.  
  174. void GetFileList( void )
  175. {
  176.   BPTR FL;
  177.   struct FileInfoBlock *FB;
  178.   LONG noerror;
  179.  
  180.  
  181.   if (FileList=AllocVec(sizeof(struct List),MEMF_PUBLIC))
  182.   {
  183.     NewList(FileList);
  184.  
  185.     if (FL=Lock(N_ND->NodeSettings.NodePlayPen,ACCESS_READ))
  186.     {
  187.       if (FB=AllocVec(sizeof(struct FileInfoBlock),MEMF_PUBLIC))
  188.       {
  189.         if (Examine(FL,FB))
  190.         {
  191.           do
  192.           {
  193.             if (noerror=ExNext(FL,FB))
  194.             {
  195.               if (FB->fib_DirEntryType<0) // File ? or DIR
  196.               {
  197.                 Files++;
  198.                 NewStrNode(FB->fib_FileName,FileList);
  199.               }
  200.             }
  201.           }
  202.           while (noerror);
  203.         }
  204.         FreeVec(FB);
  205.       }
  206.  
  207.       UnLock(FL);
  208.     }
  209.   }
  210. }
  211.  
  212. void GetFile( char *fname )
  213. {
  214.   BPTR FL;
  215.   struct FileInfoBlock *FB;
  216.  
  217.   strcpy(outstr,N_ND->NodeSettings.NodePlayPen);
  218.   strcat(outstr,fname);
  219.   if (FL=Lock(outstr,ACCESS_READ))
  220.   {
  221.     if (FB=AllocVec(sizeof(struct FileInfoBlock),MEMF_PUBLIC))
  222.     {
  223.       if (Examine(FL,FB))
  224.       {
  225.         file_name=AllocVec(50,MEMF_PUBLIC);
  226.         strcpy(file_name,FB->fib_FileName);
  227.         file_size=FB->fib_Size;
  228.         file_corrupt=FALSE;
  229.         file_sysop=FALSE;
  230.         file_incorrect=FALSE;
  231.         file_conf=0;
  232.         if (N_ND->CurrentConf) //are we in a conf ?
  233.         {
  234.           file_conf=N_ND->CurrentConf->ConfNum;
  235.         }
  236.       }
  237.       FreeVec(FB);
  238.     }
  239.     UnLock(FL);
  240.   }
  241. }
  242.  
  243. void DoorCleanup( void )
  244. {
  245.   if (FileList)
  246.   {
  247.     FreeStrList(FileList); // love those helpful library routines! :-)
  248.   }
  249. }
  250.  
  251.  
  252. BOOL CheckChars(char *charsallowed, char *checkstr)
  253. {
  254.  
  255.   // weird function...  you pass it a list of chars that are allowed in your string, if
  256.   // a char apprears in your string that's not in your list of chars then we return FALSE
  257.  
  258.   int loop,len;
  259.   BOOL foundbadchar=FALSE;
  260.  
  261.   len=strlen(checkstr);
  262.  
  263.   for (loop=0;loop<len && !foundbadchar;loop++)
  264.   {
  265.     foundbadchar=(strchr(charsallowed,checkstr[loop]) == 0);
  266.   }
  267.  
  268.   return(foundbadchar);
  269.  
  270. }
  271.  
  272. BOOL CheckFileName ( UBYTE *filename,BOOL PromptUser)
  273. {
  274.   char *allowedchars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqurstuvwxyz1234567890-_@!.";
  275.  
  276.   if (strlen(filename)>12 || filename[0]=='.' || CheckChars(allowedchars,filename))
  277.   {
  278.     if (filename[0]=='.') filename[0]='_';
  279.     filename[12]=0; //max 12 chars..
  280.  
  281.     if (PromptUser && UserAround)
  282.     {
  283.       sprintf(outstr,"Please enter a new name for the file called \"%s\"\r\n"ANSI_FG_YELLOW "NewName "ANSI_FG_BLUE": "ANSI_FG_WHITE,filename);
  284.       DOOR_WriteText(outstr);
  285.       strcpy(N_ND->CharsAllowed,allowedchars);
  286.       DOOR_GetLine(GL_EDIT|GL_DISPLAY|GL_HISTORY|GL_USECHARS,'\0',12,30,filename);
  287.       if (N_ND->OnlineStatus==OS_OFFLINE)
  288.       {
  289.         return(FALSE);
  290.       }
  291.       else
  292.       {
  293.         RemoveSpaces(N_ND->CurrentLine);
  294.         if (N_ND->CurrentLine[0])
  295.         {
  296.           strcpy(filename,N_ND->CurrentLine);
  297.           return(CheckFileName(filename,FALSE));
  298.         }
  299.       }
  300.     }
  301.     return(TRUE); //filename has been made ok..
  302.                   //might be the same as another file in the playpen tho....
  303.                   // *C*!
  304.   }
  305.   return(TRUE);
  306. }
  307.  
  308. void dorename(UBYTE *fromname,UBYTE *toname)
  309. {
  310.   UBYTE fromfull[BIG_STR],tofull[BIG_STR];
  311.  
  312.   ULONG Num=0;
  313.  
  314.   if (stricmp(fromname,toname)!=0)
  315.   {
  316.  
  317.     strcpy(fromfull,N_ND->NodeSettings.NodePlayPen);
  318.     strcat(fromfull,fromname);
  319.  
  320.     strcpy(tofull,N_ND->NodeSettings.NodePlayPen);
  321.     strcat(tofull,toname);
  322.  
  323.     if (PathOK(tofull))
  324.     {
  325.       // file exists!, so find a new name for the file!
  326.  
  327.       do
  328.       {
  329.         toname[9]=0;
  330.         sprintf(toname+strlen(toname),"%ld",Num++);
  331.  
  332.         strcpy(tofull,N_ND->NodeSettings.NodePlayPen);
  333.         strcat(tofull,toname);
  334.  
  335.       } while (PathOK(tofull) && Num<256);
  336.  
  337.       Rename(fromfull,tofull);
  338.  
  339.       // there's hardly likely to be 255 files in the pp
  340.  
  341.     }
  342.     else
  343.     {
  344.       Rename(fromfull,tofull);
  345.     }
  346.   }
  347. }
  348.  
  349. void DoorMain(int argc,char *argv[])
  350. {
  351.   LONG loop,loop2; // *C* remove all references to loop2 and tmpname!
  352.   char tmpname[20],extname[20],oldname[255];
  353.   struct List *FileID=NULL;
  354.   struct ConfData *conf;
  355.  
  356.   if (iposition("NOUSER",N_ND->ActiveDoor->SystemOptions)>=0) UserAround=FALSE;
  357.  
  358.   if (UserAround) DOOR_WriteText(ANSI_RESET "Checking uploaded files..\r\n");
  359.  
  360.   if (stricmp("ALL",argv[2])==0)
  361.   {
  362.     checkall=TRUE;
  363.     GetFileList();
  364.   }
  365.   else Files=1;  //now we can for/next loop on the files variable..
  366.  
  367.   for (loop=1;loop<=Files;loop++)
  368.   {
  369.     if (checkall)
  370.     {
  371.       GetFile(HBBS_ListName(FileList,loop-1));
  372.     }
  373.     else GetFile(argv[3]);
  374.  
  375.     // ok, file_name and file_size contain the right details about the file that
  376.     // has been uploaded into the playpen, so let's check em!
  377.  
  378.  
  379.  
  380.     // Create tmpname and extname strings..
  381.  
  382.     strcpy(oldname,file_name);
  383.  
  384.     // check to see if the file has no name (E.G. a file called ".BANANA" would be invalid)
  385.     if (!CheckFileName(file_name,TRUE))
  386.     {
  387.       dorename(oldname,file_name);
  388.       if (UserAround) DOOR_WriteText(ANSI_FG_RED "Invalid Upload\r\n" ANSI_RESET);
  389.  
  390.       sprintf(outstr,"%sBAD %s",UserAround ? "" : "NOUSER ",file_name); // move NOUSER to the door's system options!!!
  391.       DOOR_SystemDoor("MoveFile",outstr);
  392.  
  393.     }
  394.     else
  395.     {
  396.       dorename(oldname,file_name);
  397.       for (loop2=strlen(file_name)-1;loop2>=0 && file_name[loop2]!='.';loop2--);
  398.       strNcpy(tmpname,file_name,loop2);  // just the <filename> without the .ext
  399.       strfcpy(extname,file_name,loop2+1); // the .extension name without the .
  400.  
  401.  
  402.       DOOR_Add_Last_Upload(file_name);
  403.       sprintf(outstr,"%s %sWork/ %s %s %s",file_name,N_ND->NodeLocation,N_ND->NodeSettings.NodePlayPen,tmpname,extname);
  404.       DOOR_SystemDoor("Extract_DIZ",outstr);
  405.  
  406.       // Make a copy of the .DIZ and call it .ADD
  407.  
  408.       sprintf(outstr,"%sWork/%s.DIZ",N_ND->NodeLocation,file_name);
  409.       if (FileID=HBBS_LoadFile(outstr))
  410.       {
  411.         sprintf(outstr,"%s %sWork/ %s %s %s",file_name,N_ND->NodeLocation,N_ND->NodeSettings.NodePlayPen,tmpname,extname);
  412.         DOOR_SystemDoor("Modify_DIZ",outstr);
  413.       }
  414.       else
  415.       {
  416.         sprintf(outstr,"%s %sWork/ %s %s %s",file_name,N_ND->NodeLocation,N_ND->NodeSettings.NodePlayPen,tmpname,extname);
  417.         DOOR_SystemDoor("New_DIZ",outstr);
  418.  
  419.         sprintf(outstr,"%sWork/%s.DIZ",N_ND->NodeLocation,file_name);
  420.         FileID=HBBS_LoadFile(outstr);
  421.       }
  422.  
  423.       if (FileID)
  424.       {
  425.         sprintf(outstr,"%sWork/%s.ADD",N_ND->NodeLocation,file_name);
  426.         HBBS_SaveFile(outstr,FileID); // *C* error checking..?
  427.  
  428.         FreeStrList(FileID);
  429.         FileID=NULL;
  430.       }
  431.  
  432.       DOOR_SystemDoor("Check_Corrupt",file_name);
  433.       if (stricmp("CORRUPT",N_ND->DoorReturn)==0)
  434.       {
  435.         file_corrupt=TRUE;
  436.         if (UserAround) DOOR_WriteText("Moving to BAD dir!\r\n");
  437.  
  438.         sprintf(outstr,"BAD %sWork/%s.DIZ %s",N_ND->NodeLocation,file_name, file_name);
  439.         DOOR_SystemDoor("AddDIZToList",outstr);
  440.  
  441.         sprintf(outstr,"%s %sWork/ %s %s %s",file_name,N_ND->NodeLocation,N_ND->NodeSettings.NodePlayPen,tmpname,extname);
  442.         DOOR_SystemDoor("Update_DIZ",outstr);
  443.  
  444.         sprintf(outstr,"%sBAD %s",UserAround ? "" : "NOUSER ",file_name);
  445.         DOOR_SystemDoor("MoveFile",outstr);
  446.  
  447.       }
  448.       else
  449.       {
  450.         DOOR_SystemDoor("Confirm_UL",file_name);
  451.  
  452.         if (iposition("SYSOP",N_ND->DoorReturn)>=0) file_sysop=TRUE;
  453.         if (iposition("INCORRECT",N_ND->DoorReturn)>=0) file_incorrect=TRUE;
  454.  
  455.         DOOR_SystemDoor("DupeCheck",file_name);
  456.         if (iposition("DUPLICATE",N_ND->DoorReturn)>=0) file_incorrect=TRUE;
  457.  
  458.         if (!file_sysop && !file_incorrect)
  459.         {
  460.  
  461.           if (DOOR_SystemDoor("Ask_CONF",file_name))
  462.           {
  463.             file_conf=atoi(N_ND->DoorReturn);
  464.           }
  465.  
  466.           if (file_conf>=1 && file_conf<=BBSGlobal->Conferences)
  467.           {
  468.             if (UserAround)
  469.             {
  470.               DOOR_WriteText(ANSI_FG_WHITE "Placing File in the \"" ANSI_FG_BLUE);
  471.               DOOR_WriteText(HBBS_ListName(BBSGlobal->ConfList,file_conf-1));
  472.               DOOR_WriteText(ANSI_FG_WHITE "\" conference.\r\n");
  473.             }
  474.  
  475.             sprintf(outstr,"%d %sWork/%s.DIZ %s",file_conf,N_ND->NodeLocation,file_name, file_name);
  476.             DOOR_SystemDoor("AddDIZToList",outstr);
  477.             //AddDIZToList <confnum> <diz_file> <file_name>
  478.  
  479.             sprintf(outstr,"%d %s",file_conf,file_name);
  480.             DOOR_SystemDoor("AddCreds",outstr);
  481.             //AddCreds <conf> <full_filename>
  482.  
  483.             sprintf(outstr,"%s %sWork/ %s %s %s",file_name,N_ND->NodeLocation,N_ND->NodeSettings.NodePlayPen,tmpname,extname);
  484.             DOOR_SystemDoor("Update_DIZ",outstr);
  485.  
  486.             conf=(struct ConfData *)GetNode(BBSGlobal->ConfList,file_conf-1); // list index=0
  487.  
  488.             sprintf(outstr,"%s%d %s",UserAround ? "" : "NOUSER ",file_conf,file_name);
  489.             if (conf->KeepDIZWithFile)
  490.             {
  491.               sprintf(outstr+strlen(outstr)," MOVEDIZ %s",file_name);
  492.             }
  493.             DOOR_SystemDoor("MoveFile",outstr);
  494.             //MoveFile [NOUSER] <confnum> <full_filename> [MOVEDIZ filename]
  495.  
  496.             N_ND->User.CallData.LastUploadDate=time(NULL);
  497.             N_ND->User.NormalData.LastUploadDate=time(NULL);
  498.           }
  499.           else
  500.           {
  501.             if (UserAround) DOOR_WriteText("Erk! No Conference - Now What ?!\r\n"); // *C*
  502.           }
  503.         }
  504.         else
  505.         {
  506.           if (file_sysop || file_incorrect)
  507.           {
  508.             DOOR_WriteText("Moving File to SYSOP's HOLD dir!\r\n");
  509.  
  510.             sprintf(outstr,"%s %sWork/%s.DIZ %s",file_sysop ? "SYSOP" : "HOLD",N_ND->NodeLocation,file_name, file_name);
  511.             DOOR_SystemDoor("AddDIZToList",outstr);
  512.  
  513.             sprintf(outstr,"%s %sWork/ %s %s %s",file_name,N_ND->NodeLocation,N_ND->NodeSettings.NodePlayPen,tmpname,extname);
  514.             DOOR_SystemDoor("Update_DIZ",outstr);
  515.  
  516.             sprintf(outstr,"%sHOLD %s",UserAround ? "" : "NOUSER ",file_name);
  517.             DOOR_SystemDoor("MoveFile",outstr);
  518.           }
  519.         }
  520.       }
  521.     }
  522.     FreeStr(file_name);
  523.     file_name=NULL;
  524.   }
  525.  
  526.   DoorCleanup();
  527. }
  528.  
  529. int main(int argc,char *argv[])
  530. {
  531.   if (sscanf(argv[1],"%d",&N_NodeNum)==0)
  532.   {
  533.     printf("Invalid/No Paramaters for door!\n");
  534.     exit (20);
  535.   }
  536.   init("Check_Files");
  537.  
  538.   if (BBSGlobal=HBBS_GimmeBBS())
  539.   {
  540.     if (N_ND=HBBS_NodeDataPtr(N_NodeNum)) // this should not fail in normal circumstances..
  541.     {
  542.       DoorMain(argc,argv);
  543.     }
  544.   }
  545.   cleanup(0);
  546. }
  547.